<template>
    <el-card shadow="always" >
        <!-- <el-button type="primary" size="small" class="mb-3" @click.stop="addRule">新增</el-button> -->
        <el-tree
            style="max-width: 600px"
            :data="data"
            :props="{ label:'name',children:'child' }"
            v-loading="loading"
            node-key="id"
            :default-expanded-keys="defaultExpandedKeys"
            
            
        >
            <template #default="{ node, data }">
                <div class="custom-tree-node">
                    <el-tag size="small" :type="data.parent_id ? 'info' : 'primary'">{{ data.parent_id ? "子菜单" : "菜单" }}</el-tag>
                    <el-icon v-if="data.icon" :size="16" class="ml-2">
                        <component :is="data.icon" />
                    </el-icon>
                    <span>{{ data.name }}</span>

                    <div class="ml-auto">
                        <el-switch :modelValue="data.status" :active-value="1" :inactive-value="0" @change="handleStatusChange($event,data)"/>
                        <el-button text type="primary" size="small" @click.stop="updateRule(data)">修改</el-button>
                        <el-button text type="primary" size="small" @click.stop="addRule(data.id)">增加</el-button>
                        <el-button text type="primary" size="small" @click.stop="handleDelete(data)">删除</el-button>
                    </div>

                </div>
        </template>
       </el-tree>
        <!-- 抽屉 -->
        <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="handleSubmit">
            <el-form :model="form" ref="formRef"  label-width="80px" :inline="false">
                <el-form-item label="ID" prop="ID" v-if="form.id">
                    <el-input v-model="form.id" style="width: 36%;" disabled></el-input>
                </el-form-item>
                <el-form-item label="上级菜单" prop="rule_id">
        
                    <el-cascader v-model="form.rule_id" 
                    :options="options" 
                    :props="{value:'id', label:'name',children:'child',checkStrictly:true,emitPath:false }" 
                    placeholder="请选择上级菜单"/>
                </el-form-item>
                <el-form-item label="名称" prop="name" >
                    <el-input v-model="form.name" style="width: 36%;"></el-input>
                </el-form-item>
                <el-form-item label="菜单/规则" prop="type">
                    <el-radio-group v-model="form.type">
                        <span>{{ form.type }}</span>
                        <el-radio :value=1 border>菜单</el-radio>
                        <el-radio :value=0 border>规则</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="菜单图标" prop="icon" v-if="form.type == 1">
                    <IconSelect v-model="form.icon"/>
                </el-form-item>
                <el-form-item label="前端路由" prop="frontpath" v-if="form.type == 1 && form.rule_id > 0">
                    <el-input v-model="form.frontpath" style="width: 44%;"></el-input>
                </el-form-item>
                <el-form-item label="后端规则" prop="condition"  v-if="form.type == 0">
                    <el-input v-model="form.condition"></el-input>
                </el-form-item>
                <el-form-item label="请求方式" prop="method"  v-if="form.type == 0">
                    <el-select v-model="form.method" class="" placeholder="请选择请求方式">
                        <el-option
                        v-for="item in ['GET','POST','PUT','DELETE']"
                        :key="item"
                        :label="item"
                        :value="item"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input-number v-model="form.sort" :min="0" :max="1000" />
                </el-form-item>
               
            </el-form>
        </FormDrawer>
    </el-card>   
</template>

<script setup> 
import { ref,reactive,computed } from 'vue'
import { getALLMenu,updateMenuStatus,updateMenu,createMenu,delMenu } from '@/api/manager'
import FormDrawer from "@/components/FormDrawer.vue"
import IconSelect from "@/components/IconSelect.vue"
import { ElMessageBox } from 'element-plus'

const data  = ref([])
const loading  = ref(false)
const defaultExpandedKeys = ref([])
const getTree=()=>{
    getALLMenu()
    .then(res=>{
        console.log(JSON.stringify(res['menus']))
        data.value = res['menus']
        defaultExpandedKeys.value = res['menus'].map(o=>o.id) // 默认展开一级菜单
        // console.log(defaultExpandedKeys.value)
        options.value = res['menus']
    })
}
getTree()
// 抽屉
const editFlag = ref(0)
const options = ref([])
const formDrawerRef = ref()
const drawerTitle = computed(()=>editFlag.value ? "编辑" : "新增")
const form = reactive({
    rule_id:0,
    type:1,
    name:'',
    icon:'',
    frontpath:'',
    condition:'',
    method:"GET",
    sort:50,
    id:0

})

const addRule = (id)=>{
    editFlag.value = 0
    for (const key in form) {
        delete form[key]
        }
    form.sort = 50
    form.rule_id = id
    formDrawerRef.value.open()
}
const updateRule = (data)=>{
    console.log('修改',data)
    editFlag.value = 1
    formDrawerRef.value.open()
    form.name = data.name
    form.type = data.type
    form.rule_id = data.parent_id
    form.frontpath = data.frontpath
    form.icon = data.icon
    form.sort = data.sort
    form.id = data.id
    
}
const handleSubmit = ()=>{
    formDrawerRef.value.showLoading()
    console.log(form)
    const fun = editFlag.value ? updateMenu(form) : createMenu(form)
    fun.then(res => {
        formDrawerRef.value.close()
        // location.reload()
        getTree()
    }).finally(() => {
        formDrawerRef.value.hideLoading()
    })
}
    

// 修改状态
const handleStatusChange = (status, row) => {
    console.log(status, row)
    row.status = status
        row.statusLoading = true
        updateMenuStatus(row.id, status)
            .then(res => {             
                row.status = status
            })
            .finally(() => {
                row.statusLoading = false
            })
        }
// 删除
const handleDelete = (row)=>{
    ElMessageBox.confirm(`确认删除 ${row.name}吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        delMenu(row.id).then(res=>{
            // ElMessage.success('删除成功')
            getTree()
        })
    })
}
</script>

<style>
.custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    font-size: 14px;
    padding-right: 8px;
}
.el-tree-node__content {
    padding: 20px 0;
}
</style>